package jtk.project4.fleet.screen.employee;

import java.awt.FlowLayout;
import java.util.ArrayList;

import jtk.project4.fleet.action.AddEmployeeAction;
import jtk.project4.fleet.action.CancelAction;
import jtk.project4.fleet.action.CloseAction;
import jtk.project4.fleet.action.EditEmployeeAction;
import jtk.project4.fleet.domain.Employee;
import jtk.project4.fleet.event.EmployeeEvent;
import jtk.project4.fleet.event.EmployeeEvent.EmployeeEventType;
import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.action.ActionGroup;
import nl.coderight.jazz.dialog.MessageType;
import nl.coderight.jazz.dialog.OptionDialog;
import nl.coderight.jazz.dialog.OptionType;
import nl.coderight.jazz.dialog.ReturnOptionType;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.SearchField;
import nl.coderight.jazz.form.field.TableField;
import nl.coderight.jazz.form.field.TableField.AutoResizeMode;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.i18n.Message;
import nl.coderight.jazz.i18n.Message.Choice;
import nl.coderight.jazz.i18n.Message.Param;
import nl.coderight.jazz.view.StatusBar;
//import jtk.project4.fleet.screen.viewVendor.VendorFilter;

public class EmployeeView extends FormView{
	private TableField<Employee> employeeTableField;
	//private TableField<Employee> filterVendorField;
	
	private Action closeAction;
	
	private Action addEmployeeAction;
	private Action editEmployeeAction;
	private Action deleteEmployeeAction;
	private ActionGroup editEmployeeActionGroup;
	
	private Message status;
	private StatusBar statusBar;
	
	private PushButton helpButton;
	private PushButton closeButton;
	private PushButton addButton;
	private PushButton editButton;
	private PushButton deleteButton;
	
	private SearchField filterField;
	
	
//private VendorFilter vendorFilter;
	public EmployeeView(){
		setSize(800,600);
		center();
		// create filter for vendor table
//		vendorFilter = new VendorFilter();
	}
	public void handleEvent (EmployeeEvent evt){
		Employee selectedEmployee = employeeTableField.getValue();
		switch(evt.getType()){
		case CLOSE:
			close();
		case CHANGE:
			//bwt filter
		case SELECT :
			ArrayList<Choice> choices = new ArrayList<Choice>();
			choices.add(new Choice(0, "status.message.none"));
			choices.add(new Choice(1, "status.message.one"));
			choices.add(new Choice(2, "status.message.all"));
			
			status = new Message("status.message");
			status.addParam(new Param(employeeTableField.getValueCount()));
			status.addParam(new Param(employeeTableField.getListOfValues().size(), choices));
			status.addParam(new Param(employeeTableField.getSelectionCount()));
			// display message on status bar
			statusBar = new StatusBar();
			statusBar.setMessage(status);
	

		break;
		case DELETE:
			Message message = new Message("dialog.delete.message");
			//message.addParam(new Param(selectedEmployee.getFirst()));
			//message.addParam(new Param(selectedEmployee.getLast)));
			
			OptionDialog dialog = new OptionDialog(this);
			dialog.setTitle("dialog.delete.title");
			dialog.setOptionType(OptionType.YES_NO);
			dialog.setOptions("button.yes", "button.no", "button.cancel");
			dialog.setDefaultOption("button.no");
			dialog.setMessageType(MessageType.QUESTION);
			dialog.setMessage(message);
			dialog.show();			
			if (dialog.getReturnOptionType() == ReturnOptionType.YES) {
				propagateEvent(evt);
			}
			break;
		//case FILTER:
			// filterField is changed, update filter
			//vendorFilter.setValue(filterField.getValue());
		//	break;
			
		default:
			// propagate event to controller
			propagateEvent(evt);
			break;
		

		}
	}

	@Override
	protected void init() {
		// TODO Auto-generated method stub
		createActions();
		createFields();
		createLayout();		
	}
	private void createActions() {
		// TODO Auto-generated method stub
		addEmployeeAction = new AddEmployeeAction();
		//editEmployeeAction = new EditEmployeeAction();
		//deleteEmployeeAction = new DeleteVendorAction();
		closeAction = new CloseAction();
		
		//editEmployeeActionGroup = new ActionGroup();
		//editEmployeeActionGroup.addAction(editEmployeeAction);
		//editEmployeeActionGroup.addAction(deleteEmployeeAction);
	}
	
	private void createFields() {		// TODO Auto-generated method stub
//				filterField = new SearchField(25);
//				filterField.setInputPrompt("filter.prompt");
//				filterField.setOnSearchEvent(new EmployeeEvent(EmployeeEventType.FILTER));
//				
				employeeTableField = new TableField<Employee>("employees", "employee");
				employeeTableField.setTitle("employee.header.table");
				employeeTableField.setAutoResizeMode(AutoResizeMode.OFF);
				employeeTableField.getSelectionValues();			
				
			//	employeeTableField.setOnSelectEvent(new EmployeeEvent(EmployeeEventType.SELECT));
			//	employeeTableField.setOnChangeEvent(new EmployeeEvent(EmployeeEventType.CHANGE));
			//	employeeTableField.setOnEditEvent(new EmployeeEvent(EmployeeEventType.EDIT));
			//	employeeTableField.setOnSelectGroup(editEmployeeActionGroup);	
				
				employeeTableField.addColumn("label.name","name");
				employeeTableField.addColumn("label.type","type");
				employeeTableField.addColumn("label.contact","contact");
				employeeTableField.addColumn("label.address1","address1");
				employeeTableField.addColumn("label.address2","address2");
				employeeTableField.addColumn("label.city","city");
				employeeTableField.addColumn("label.stateProvince","stateProvince");
				employeeTableField.addColumn("label.postalCode","postalCode");
				employeeTableField.addColumn("label.country","country");
				employeeTableField.addColumn("label.phone1","phone1");
				employeeTableField.addColumn("label.phone2","phone2");
				employeeTableField.addColumn("label.fax","fax");
				employeeTableField.addColumn("label.email","email");
				employeeTableField.addColumn("label.website","website");
				employeeTableField.addColumn("label.terms","terms");
				employeeTableField.addColumn("label.location","locID");
				employeeTableField.addColumn("label.comments","comments");	
//				employeeTableField.addFilter(vendorFilter);
				
				
				helpButton = new PushButton("Help");
				closeButton = new PushButton(closeAction);
				
				addButton = new PushButton(addEmployeeAction);
				addButton.setToolTipText(addEmployeeAction.getLongDescription());
				
			//	editButton = new PushButton(editEmployeeAction);
			//	editButton.setToolTipText(editEmployeeAction.getLongDescription());
				
			//	deleteButton = new PushButton(deleteEmployeeAction);
			//	deleteButton.setToolTipText(deleteEmployeeAction.getLongDescription());								
	}
	private GroupControl createForm() {
		GroupControl form = new GroupControl();
		form.addField(employeeTableField, NORTH);
		form.addField(createButtons(), SOUTH);		
		return form;
	}
	
	private void createLayout() {
		addField(createForm());
	}
	

	private GroupControl createFieldsGroup() {
		// TODO Auto-generated method stub
		GroupControl fieldGroup = new GroupControl();
		fieldGroup.addField(employeeTableField, NORTH);
		fieldGroup.addField(createToolBar(),CENTER);
		fieldGroup.addField(createButtons(), SOUTH);
		return fieldGroup;
	}
	private GroupControl createToolBar() {
		// TODO Auto-generated method stub
		GroupControl buttonGroup = new GroupControl();
		buttonGroup.setLayout(new FlowLayout(FlowLayout.LEFT));
		buttonGroup.addField(addButton);
		buttonGroup.addField(editButton);
		buttonGroup.addField(deleteButton);
		buttonGroup.addField(filterField);
		return buttonGroup;
	}

	private GroupControl createButtons(){
		GroupControl buttonGroup = new GroupControl();
		buttonGroup.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttonGroup.addField(addButton);
		buttonGroup.addField(helpButton);
		buttonGroup.addField(closeButton);
		return buttonGroup;
	}
}
